package com.tedu.bbs.controller;

import com.tedu.bbs.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

@Controller
public class ArticleController {
    @RequestMapping("articleContent")
    public void articleContent(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理文章正文");
        String cId=request.getParameter("cId");
        int cid=Integer.parseInt(cId);

        try(
                Connection connection=  DBUtil.getConnection();
        ){
            String sql="SELECT content FROM article WHERE id=?";
            PreparedStatement ps= connection.prepareStatement(sql);
            ps.setInt(1,cid);
            ResultSet rs= ps.executeQuery();
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw=response.getWriter();
            if (rs.next()){
                String content=rs.getString(1);
                pw.println("<!DOCTYPE html>");
                pw.println("<html lang=\"en\">");
                pw.println("<head>");
                pw.println("    <meta charset=\"UTF-8\">");
                pw.println("    <title>Title</title>");
                pw.println("</head>");
                pw.println("<body>");
                pw.println("<center>");
                pw.println("  <h2>"+content+"</h2>");
                pw.println("</center>");
                pw.println("</body>");
                pw.println("</html>");
            }

        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }

    }


    @RequestMapping("/articleList")
    public void articleLise(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理文章列表");
        String uid = request.getParameter("userId");
        System.out.println("该用户的id是:"+uid);
        try(
                Connection connection = DBUtil.getConnection();
        ){
            /*
                SELECT u.name,a.id,a.title FROM userinfo u,article a WHERE u.id=a.u_id AND a.u_id=?
             */
            String sql = "SELECT u.username,a.id,a.title " +
                    "FROM userinfo u,article a " +
                    "WHERE u.id=a.u_id " +
                    "AND a.u_id=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1,Integer.parseInt(uid));
            ResultSet rs = ps.executeQuery();

            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("    <meta charset=\"UTF-8\">");
            pw.println("    <title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("    <center>");
            pw.println("        <h1>文章列表</h1>");
            pw.println("        <table border=\"1\">");
            pw.println("            <tr>");
            pw.println("                <td>标题</td>");
            pw.println("                <td>作者</td>");
            pw.println("            </tr>");
            while(rs.next()) {
                String uname = rs.getString(1);
                int aid = rs.getInt(2);
                String title = rs.getString(3);
                pw.println("<tr>");
                pw.println("<td><a href='/articleContent?cId="+aid+"'>"+rs.getString("title")+"</a></td>");
                pw.println("<td>"+uname+"</td>");
                pw.println("</tr>");
            }
            pw.println("        </table>");
            pw.println("    </center>");
            pw.println("</body>");
            pw.println("</html>");





        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }


    }


    @RequestMapping("/writeArticle")
    public void write(HttpServletRequest request, HttpServletResponse response){
        String title =request.getParameter("title");
        String author =request.getParameter("author");
        String content =request.getParameter("content");

        try (Connection connection = DBUtil.getConnection()
        ){
            String sql ="SELECT id FROM userinfo WHERE username =? ";
            PreparedStatement ps  =connection.prepareStatement(sql);
            ps.setString(1,author);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                int id =rs.getInt(1);
                String line ="INSERT INTO article(title,content,u_id) VALUES (?,?,?)";
                PreparedStatement ps1 =connection.prepareStatement(line);
                ps1.setString(1,title);
                ps1.setString(2,content);
                ps1.setInt(3,id);
                int num=ps1.executeUpdate();
                if (num>0){
                    response.sendRedirect("Article_success.html");
                }else {
                    response.sendRedirect("ArticleNot.html");
                }

            }else {
                response.sendRedirect("ArticleNot.html");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
